<template>
  <ma-form-item
    v-if="typeof props.component.display == 'undefined' || props.component.display === true"
    :component="props.component"
    :custom-field="props.customField"
  >
    <slot :name="`form-${props.component.dataIndex}`" v-bind="props.component">
      <a-checkbox-group
        v-model="value"
        :size="props.component.size"
        :max="props.component.max"
        :direction="props.component.direction"
        :disabled="props.component.disabled"
        @change="rv('onChange', $event)"
      >
        <template v-for="(item, index) in dictList[dictIndex] ?? []">
          <a-checkbox :value="item.value" :disabled="item.disabled" :indeterminate="item.indeterminate">{{
            item.label
          }}</a-checkbox>
        </template>
      </a-checkbox-group>
    </slot>
  </ma-form-item>
</template>

<script setup>
import { ref, inject, onMounted, watch } from "vue"
import { get, set } from "lodash"
import MaFormItem from "./form-item.vue"
import { runEvent } from "../js/event.js"

const props = defineProps({
  component: Object,
  customField: { type: String, default: undefined }
})

const formModel = inject("formModel")
const dictList = inject("dictList")
const getColumnService = inject("getColumnService")
const columns = inject("columns")
const rv = async (ev, value = undefined) =>
  await runEvent(props.component, ev, { formModel, getColumnService, columns }, value)
const index = props.customField ?? props.component.dataIndex
const dictIndex = index.match(/^(\w+\.)\d+\./)
  ? index.match(/^(\w+\.)\d+\./)[1] + props.component.dataIndex
  : props.component.dataIndex
const value = ref(get(formModel.value, index))

watch(
  () => get(formModel.value, index),
  (vl) => (value.value = vl)
)
watch(
  () => value.value,
  (v) => {
    set(formModel.value, index, v)
    index.indexOf(".") > -1 && delete formModel.value[index]
  }
)

rv("onCreated")
onMounted(() => rv("onMounted"))
</script>
